<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use app\common\controller\Base;
use utils\Jssdk;
use utils\Page;
use think\Db;
use think\captcha\Captcha;
// 
use think\Cookie;
use think\Session;

/**
 * @title 用于功能测试
 */
class Test extends Base {

    /**
     * 写cookies
     */
    public function cookie_set() {
        
        $username = input('post.username');
        $password = input('post.password');


        Cookie::set('uid', '1111');
        Cookie::set('token', $username);
    }

    public function cookie_clear() {

        Session::set('a', 'aaa');


        Cookie::delete('uid');
        Cookie::delete('token');
    }

    /**
     * @title 设置了cookies才能访问的页面
     */
    public function cookie() {


        $token = Cookie::has('token');

        if (!$token) {

            echo 'cookies null';
        } else {

            $token_value = Cookie::get('token');

            if ($token_value == 'zxzmww') {
                echo 'cookies ok';
            } else {
                echo 'cookies error';
            }
        }
    }

    //
    public function nfc($code) {

        // http://democms.mmno.com/nfc?code=2b63LG+nN4xJQoSVxQIcoC5B7aaGDOpZq9zee1OfE7c2eVmu


        echo $code;
    }
    
    
    
    
    

    /**
     * @title 用于自动下拉
     */
    public function load_article_json() {

        $keyword = request()->get('keyword');
        $keyword = trim($keyword);


        $lists = db('article')->where('title', 'like', '%' . $keyword . '%')->field('id,title as label,title as value,image')->select();
        foreach ($lists as $key => $value) {
            if ($value['image'])
                $lists[$key]['image'] = APP_HOST . img_resize($value['image'], 50, 50);
            else
                $lists[$key]['image'] = APP_HOST . img_resize('image/noimage.jpg', 50, 50);
        }

        echo json_encode($lists);
    }

    /**
     * @title 添加一个会员日志-使用事务的方式
     */
    public function member_log_add() {

        // 启动事务
        Db::startTrans();
        try {

            $member_id = 1;

            Db::name('member_log')->insert([
                'ip' => get_client_ip(),
                'member_id' => $member_id
            ]);


            $count = Db::name('member_log')->where('member_id', $member_id)->count();

            Db::name('member')->where('id', $member_id)->setField('hits', $count);

            Db::name('member')->insert([
                'id' => 1,
            ]);


            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();

            dump($e->getMessage());
        }
    }

    /**
     * @title 事务代码的测试
     * @return type
     */
    public function member_log() {


        $lists = model('member_log')->select();

        dd($lists);








        return view();
    }

}
